OSM Traces (GPX files)¶

Binder IPYNB HTML

This notebook illustrates the use of GPS traces shared publicly by OSM community members in GPX format.

In [1]:
import pandas as pd
import geopandas as gpd
import movingpandas as mpd

from os.path import exists
from urllib.request import urlretrieve
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta

import warnings
warnings.simplefilter("ignore")
INFO: Missing optional dependencies. To use the trajectory smoother classes please install Stone Soup (see https://stonesoup.readthedocs.io/en/latest/#installation).
In [2]:
mpd.__version__
Out[2]:
'0.9.rc3'

Download OSM traces and generate a GeoDataFrame¶

In [3]:
def get_osm_traces(page=0, bbox='16.18,48.09,16.61,48.32'):
    file = 'osm_traces.gpx'
    url = f'https://api.openstreetmap.org/api/0.6/trackpoints?bbox={bbox}&page={page}'
    if not exists(file):
        urlretrieve(url, file)
    gdf = gpd.read_file(file, layer='track_points')
    # OPTIONAL: dropping empty columns
    gdf.drop(columns=['ele', 'course', 'speed', 'magvar', 'geoidheight', 'name', 'cmt', 'desc',
       'src', 'url', 'urlname', 'sym', 'type', 'fix', 'sat', 'hdop', 'vdop',
       'pdop', 'ageofdgpsdata', 'dgpsid'], inplace=True) 
    return gdf

TrajectoryCollection from OSM traces GeoDataFrame¶

In [4]:
gdf = get_osm_traces()
osm_traces = mpd.TrajectoryCollection(gdf, 'track_fid', t='time')
print(f'The OSM traces download contains {len(osm_traces)} tracks')
The OSM traces download contains 1 tracks
In [5]:
for track in osm_traces: print(f'Track {track.id}: length={track.get_length():.0f}m')
Track 0: length=3964m
In [6]:
track.plot()
Out[6]:
<AxesSubplot:>

Generalizing and visualizing¶

Generalization is optional but speeds up rendering

In [7]:
osm_traces = mpd.MinTimeDeltaGeneralizer(osm_traces).generalize(tolerance=timedelta(minutes=1))
osm_traces.hvplot(title='OSM Traces', line_width=7, width=700, height=400)
Out[7]:
In [8]:
osm_traces.get_trajectory(0).hvplot(title='Speed (m/s) along track', c='speed', cmap='RdYlBu',
                                    line_width=7, width=700, height=400, tiles='CartoLight', colorbar=True)
Out[8]:

Continue exploring MovingPandas¶

  1. Bird migration analysis
  2. Ship data analysis
  3. Horse collar data exploration
  4. OSM traces
  5. Soccer game
  6. Mars rover & heli
In [ ]: